package com.hemaapp.yjnh.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.R;

import java.io.IOException;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 引导页，四张图片
 */
public class WelcomeActivity extends BaseActivity {

    @Bind(R.id.view_pager)
    ViewPager viewPager;
    @Bind(R.id.radio_group)
    RadioGroup radioGroup;

    private String[] imgs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        ButterKnife.bind(this);
        imgs = new String[] { "introduction_1.png",
                "introduction_2.png", "introduction_3.png",
                "introduction_4.png" };
        radioGroup.removeAllViews();
        int size = ((BitmapDrawable) getResources().getDrawable(R.drawable.dot_n)).getBitmap().getWidth();
        for (int i = 0; i < imgs.length; i++) {
            RadioButton button = new RadioButton(this);
            button.setButtonDrawable(R.drawable.ad_show_indicator);
            button.setId(i);
            button.setClickable(false);
            LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
                    (i == imgs.length - 1) ? size : size * 2, size);
            button.setLayoutParams(params2);
            if (i == 0)
                button.setChecked(true);
            radioGroup.addView(button);
        }
        viewPager.setOffscreenPageLimit(imgs.length);
        viewPager.setAdapter(new ImageViewPagerAdapter(this));
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                RadioButton rbt = (RadioButton) radioGroup.getChildAt(position);
                if (rbt != null)
                    rbt.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public class ImageViewPagerAdapter extends PagerAdapter {

        private Context context;

        public ImageViewPagerAdapter(Context context) {
            this.context = context;
        }

        @Override
        public int getCount() {
            return imgs.length;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }
        @Override
        public Object instantiateItem(final ViewGroup container, int position) { // 这个方法用来实例化页卡
            final ImageView imageView;
            if (container.getChildAt(position) == null) {
                imageView = new ImageView(this.context);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT);
                imageView.setLayoutParams(params);
                try {
                    BitmapDrawable d = new BitmapDrawable(this.context.getAssets()
                            .open(imgs[position]));
                    imageView.setImageDrawable(d);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                imageView.setTag(position);
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = (int) imageView.getTag();
                        if (pos == imgs.length-1) {
                            Intent it = new Intent(context, MainActivity.class);
                            startActivity(it);
                            finish();
                        }
                    }
                });
                container.addView(imageView);
            } else
                imageView = (ImageView) container.getChildAt(position);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
